home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / InvisibleButton.java < prev    next >
Text File  |  1998-08-27  |  8KB  |  242 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Rectangle;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.ActionListener;
  11. import java.beans.PropertyVetoException;
  12. import java.beans.PropertyChangeListener;
  13. import java.beans.VetoableChangeListener;
  14. import java.awt.AWTEvent;
  15. import java.awt.AWTEventMulticaster;
  16.  
  17. //    01/02/97    RKM    Changed paintComponent to not cause recursive drawing on overlap
  18. //    01/02/97    RKM    Changed paintComponent to flush image after it is used
  19. //    01/16/97    RKM    Added override of update to avoid flicker
  20. //    01/18/07    RKM    Added more documentation and checked return from img.getGraphics
  21. //    01/29/97    TWB    Integrated changes from Windows and RKM's changes
  22. //     01/29/97    TWB    Integrated changes from Macintosh
  23. //     02/01/97    RKM    Extracted transparency drawing trick into TransparencyTrickUtils
  24. //     03/21/97    LAB    Updated to Java 1.1.
  25. //     07/14/97    LAB    Added add/removeNotify for event listener registration.
  26. //                    Removed use of TransparencyTrick and made Lightweight.
  27. //                    Added version and author tags.
  28. //                    Removed paint() and update() as they are not needed.
  29. //  07/29/97    CAR marked fields transient as needed
  30. //                  inner adaptor classes implement java.io.Serializable
  31.  
  32. /**
  33.  * Use this component to create an invisible area, usually within an image,
  34.  * that initiates an action when clicked. Specifically, use InvisibleButton to
  35.  * <UL>
  36.  * <DT>╖ Create a "hot spot" on an image or on a component.</DT>
  37.  * <DT>╖ Accept or yield focus.</DT>
  38.  * <DT>╖ Respond to a user event.</DT>
  39.  * <DT>╖ Send an action event to another component.</DT>
  40.  * </UL>
  41.  * Button tips:
  42.  * <UL>
  43.  * <DT>╖ Buttons accept and yield focus automatically by default.</DT>
  44.  * <DT>╖ Buttons accept clicked events automatically by default.</DT>
  45.  * <DT>╖ To send an action event to another component, use the Interaction
  46.  * Wizard. Optionally, you can override the InvisibleButtonÆs click event in
  47.  * project source code.</DT>
  48.  * <DT>╖ Overlapping components - browsers handle components layered on each
  49.  * other differently. Some browsers display/layer the InvisibleButtons on top
  50.  * of a particular component, while others display them underneath. Therefore,
  51.  * it often requires two sets of InvisibleButtons to ensure that one is "on top";
  52.  * one on top of the particular component and the other underneath.
  53.  * </UL>
  54.  *
  55.  * @version 1.1, July 14, 1997
  56.  * @author Symantec
  57.  */
  58. public class InvisibleButton extends Component
  59. {
  60.     /**
  61.      * Constructs an InvisibleButton.
  62.      */
  63.     public InvisibleButton()
  64.     {
  65.         pressed = false;
  66.     }
  67.  
  68.     /**
  69.      * Tells this component that it has been added to a container.
  70.      * This is a standard Java AWT method which gets called by the AWT when
  71.      * this component is added to a container. Typically, it is used to
  72.      * create this component's peer.
  73.      *
  74.      * It has been overridden here to hook-up event listeners.
  75.      *
  76.      * @see #removeNotify
  77.      */
  78.     public synchronized void addNotify()
  79.     {
  80.         super.addNotify();
  81.  
  82.         //Hook up listeners
  83.         if (mouse == null)
  84.         {
  85.             mouse = new Mouse();
  86.             addMouseListener(mouse);
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Tells this component that it is being removed from a container.
  92.      * This is a standard Java AWT method which gets called by the AWT when
  93.      * this component is removed from a container. Typically, it is used to
  94.      * destroy the peers of this component and all its subcomponents.
  95.      *
  96.      * It has been overridden here to unhook event listeners.
  97.      *
  98.      * @see #addNotify
  99.      */
  100.     public synchronized void removeNotify()
  101.     {
  102.         //Unhook listeners
  103.         if (mouse != null)
  104.         {
  105.             removeMouseListener(mouse);
  106.             mouse = null;
  107.         }
  108.         super.removeNotify();
  109.     }
  110.  
  111.     /**
  112.      * Sets the command name of the action event fired by this button.
  113.      * @param command the name of the action event command fired by this button
  114.      * @exception PropertyVetoException
  115.      * if the specified property value is unacceptable
  116.      */
  117.     public void setActionCommand(String command) throws PropertyVetoException
  118.     {
  119.         String oldValue = actionCommand;
  120.  
  121.         vetos.fireVetoableChange("ActionCommand", oldValue, command);
  122.         actionCommand = command;
  123.         changes.firePropertyChange("ActionCommand", oldValue, command);
  124.     }
  125.  
  126.     /**
  127.      * @return the command name of the action event fired by this button.
  128.      */
  129.     public String getActionCommand()
  130.     {
  131.         return actionCommand;
  132.     }
  133.  
  134.     /**
  135.      * Adds the specified action listener to receive action events
  136.      * from this button.
  137.      * @param l the action listener
  138.      */
  139.     public synchronized void addActionListener(ActionListener l)
  140.     {
  141.         actionListener = AWTEventMulticaster.add(actionListener, l);
  142.     }
  143.  
  144.     /**
  145.      * Removes the specified action listener so it no longer receives
  146.      * action events from this button.
  147.      * @param l the action listener
  148.      */
  149.     public synchronized void removeActionListener(ActionListener l)
  150.     {
  151.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  152.     }
  153.  
  154.     /**
  155.      * Adds a listener for all event changes.
  156.      * @param PropertyChangeListener listener the listener to add.
  157.      * @see #removePropertyChangeListener
  158.      */
  159.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  160.     {
  161.         changes.addPropertyChangeListener(listener);
  162.     }
  163.  
  164.     /**
  165.      * Removes a listener for all event changes.
  166.      * @param PropertyChangeListener listener the listener to remove.
  167.      * @see #addPropertyChangeListener
  168.      */
  169.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  170.     {
  171.         changes.removePropertyChangeListener(listener);
  172.     }
  173.  
  174.     /**
  175.      * Adds a vetoable listener for all event changes.
  176.      * @param VetoableChangeListener listener the listener to add.
  177.      * @see #removeVetoableChangeListener
  178.      */
  179.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  180.     {
  181.         vetos.addVetoableChangeListener(listener);
  182.     }
  183.  
  184.     /**
  185.      * Removes a vetoable listener for all event changes.
  186.      * @param VetoableChangeListener listener the listener to remove.
  187.      * @see #addVetoableChangeListener
  188.      */
  189.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  190.     {
  191.         vetos.removeVetoableChangeListener(listener);
  192.     }
  193.  
  194.     /**
  195.      * This is the Mouse Event handling innerclass.
  196.      */
  197.     class Mouse extends java.awt.event.MouseAdapter implements java.io.Serializable
  198.     {
  199.         /**
  200.          * Handles Mouse Pressed events
  201.          * @param e the MouseEvent
  202.          */
  203.         public void mousePressed(MouseEvent e)
  204.         {
  205.             pressed = true;
  206.         }
  207.  
  208.         /**
  209.          * Handles Mouse Released events
  210.          * @param e the MouseEvent
  211.          */
  212.         public void mouseReleased(MouseEvent e)
  213.         {
  214.             if (pressed)
  215.             {
  216.                 pressed = false;
  217.                 sourceActionEvent();
  218.             }
  219.         }
  220.     }
  221.  
  222.     /**
  223.      * Fire an action event to the listeners
  224.      */
  225.     protected void sourceActionEvent()
  226.     {
  227.         if (actionListener != null)
  228.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  229.     }
  230.  
  231.     String actionCommand = "ButtonPressed";
  232.     ActionListener actionListener = null;
  233.     /**
  234.      * True if the button is currently pressed.
  235.      */
  236.     transient protected boolean pressed;
  237.     private Mouse mouse = null;
  238.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  239.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  240. }
  241.  
  242.